<div style="width:690px; float:left; margin-right:5px" class="border_radius">
<?php
	include_once("../../coms/xl_sanpham.php");
	include_once("../../include/ckeditor/ckeditor.php");
	include_once("../../include/convert_link.php");
	if(!isset($_REQUEST['key'])):
		header("location:trangchu.php");
	else:
		if(!isset($_REQUEST['malsp'])):
			header("location:trangchu.php");
		else:
			$MaLoaiSanPham = $_REQUEST['malsp'];
?>
<script type="text/javascript">
function onlyNumbers(evt)
{
	var e = event || evt; // for trans-browser compatibility
	var charCode = e.which || e.keyCode;

	if (charCode > 31 && (charCode < 48 || charCode > 57))
		return false;

	return true;

}
function show_error(id, text){
	$("#validateError").html(text);
	$("#validateError").addClass( "validateTips" );
	id.focus();
	setTimeout(function() {
		$("#validateError").removeClass( "validateTips");
		$("#validateError").html("");
	}, 3500 );
}

function Kiemtra_Them_San_Pham(form1)
{
	var myfrmObj=document.createElement("form");
	myfrmObj=form1;
	if($('input[name*="txt_TenSanPham"]').val()=="")
	{
		show_error($('input[name*="txt_TenSanPham"]'), "Tên dịch vụ không được để trống.Vui lòng nhập lại!!!");
		return false;
	}
}
</script>

<form action="trangchu.php?key=t_sp&malsp=<?php echo $MaLoaiSanPham;?>" method="post" name="frm_ins_dich_vu"  onsubmit="return Kiemtra_Them_San_Pham(this)" enctype="multipart/form-data">

<div style="width:670px; float:left; padding-left:5px;">

<?php

		$CKEditor_new = new CKEditor();
		$CKEditor_new->basePath = '../../include/ckeditor/';
		$CKEditor_new->config['height'] = 400;
		$CKEditor_new->config['width'] =680;


		$CKEditor_Title = new CKEditor();
		$CKEditor_Title->basePath = '../../include/ckeditor/';
		$CKEditor_Title->config['height'] = 200;
		$CKEditor_Title->config['width'] = 680;

		$CKEditor_KT = new CKEditor();
		$CKEditor_KT->basePath = '../../include/ckeditor/';
		$CKEditor_KT->config['height'] = 300;
		$CKEditor_KT->config['width'] = 680;
		/*Button cap nhat*/

		if(isset($_POST['btn_HoanTat'])):
			$tensanpham = $_POST['txt_TenSanPham'];
			$thuonghieu = $_POST['cb_thuonghieu'];
			$congsuat = $_POST['cb_congsuat'];
			$gia = $_POST['txt_GiaCa'];
			$mota_ngangon= $_POST['mota_ngangon'];
			$mota_daydu= $_POST['mota_daydu'];
			$thongso_kythuat = $_POST['thongso_kythuat'];
			$tags = $_POST['tags'];
			$mota = $_POST['txtmota'];
			$sanphamnoibat = $_POST['rd_noibat'];

			$MaLoaiSanPham = $_REQUEST['malsp'];

			$timezone  = +7; //(GMT +7:00)
			$ngaydang = gmdate("Y-m-d H:i:s", time() + 3600*($timezone+date("0")));

			$file = $_FILES['up_hinh']['name'];

			$kq=array(0);
			$them = new xl_sanpham();
				if($file != NULL)
				{
					$kq=up_hinh();
					if($kq[2]==true)
					{
						$tenhinhanh = "images/sanpham/".$kq[1];
						$ketqua = $them->Them_SanPham($MaLoaiSanPham, $thuonghieu, $congsuat, $tensanpham, $gia, $ngaydang, $mota_ngangon, $mota_daydu, $thongso_kythuat, $sanphamnoibat, $tenhinhanh, $tags, $mota);

						echo '<script type="text/javascript">
								alert("Thêm sản phẩm thành công!!!");
							</script>';
						echo '<meta http-equiv="refresh" content="0;url=trangchu.php?key=qlctlsp&malsp='.$MaLoaiSanPham.'"  />';
					}
				}
				else
				{
					$ketqua = $them->Them_SanPham($MaLoaiSanPham, $thuonghieu, $congsuat, $tensanpham, $gia, $ngaydang, $mota_ngangon, $mota_daydu, $thongso_kythuat, $sanphamnoibat, "", $tags, $mota);
					echo '<script type="text/javascript">
								alert("Thêm sản phẩm thành công!!!");
							</script>';

					echo '<meta http-equiv="refresh" content="0;url=trangchu.php?key=qlctlsp&malsp='.$MaLoaiSanPham.'"  />';

				}

		endif;
?>

<div class="TitlePart">
	<?php
		include_once("../../coms/xl_loaisanpham.php");
		$lsp = new xl_loaisanpham();
		$arr_lsp = $lsp->Load_LoaiSanPham_TheoMa($MaLoaiSanPham);
	?>
    <h3> <span style="color:#006">Thêm sản phẩm "<?php echo $arr_lsp['tenloaisanpham'];?>"</span> <input type="button" name="bt_lamlai" value="Quay lại"  onClick=" javascript:history.go(-1)" class="button small blue" style="float:right"/></h3>

</div>
	<div id="idvalidateTips" align="center" style="width:auto; padding-bottom:5px;">

     <span id="validateError"  style="color:#FF0000"></span></div>
   <strong>- Tên sản phẩm:&nbsp;&nbsp;</strong>

    <input type="text" name="txt_TenSanPham" style="width:550px; padding-bottom:5px" class="border_radius" />

	<strong>- Thương hiệu:&nbsp;&nbsp;&nbsp;</strong>
	<select name="cb_thuonghieu" class="border_radius">
    	<?php
			include_once("../../coms/xl_thuonghieu.php");
			$tt = new xl_thuonghieu();
			$arr_tt = $tt->LoadAll();
			for($i=0; $i<count($arr_tt);$i++):
				$row_tt = $arr_tt[$i];
				echo '<option value="'.$row_tt['mathuonghieu'].'">'.$row_tt['tenthuonghieu'].'</option>';
			endfor;
		?>
    </select>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

	<strong>- Công suất:&nbsp;&nbsp;</strong>
	<select name="cb_congsuat" class="border_radius">
    	<?php
			include_once("../../coms/xl_congsuat.php");
			$cs = new xl_congsuat();
			$arr_cs = $cs->LoadAll();
			for($i=0; $i<count($arr_cs);$i++):
				$row_cs = $arr_cs[$i];
				echo '<option value="'.$row_cs['macongsuat'].'">'.$row_cs['tencongsuat'].'</option>';
			endfor;
		?>
    </select><br />

	 <strong>- Giá cả:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</strong>

    <input type="text" name="txt_GiaCa" style="padding-bottom:5px" class="border_radius" onkeypress="return onlyNumbers();" /><br />
	<strong>- Hình ảnh:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</strong>

    <input type="file" name="up_hinh" class="border_radius"/><br />
	<strong>- Sản phẩm nổi bật:&nbsp;&nbsp;&nbsp;&nbsp;</strong>
	<input type="radio" name="rd_noibat" value="1" /> Có &nbsp;&nbsp;
    <input type="radio" name="rd_noibat" value="0" checked="checked"/> Không<br />
    <strong>- Mô tả ngắn gọn:<br /></strong>
    <?php
		include_once("../../coms/xl_loaitrang.php");
		$lt = new xl_loaitrang();
		$mota_ngangon = $lt->Load_FormMoTaNganGon();
		$CKEditor_Title->editor("mota_ngangon",$mota_ngangon['noidung']);
	?><br />

    <strong>- Mô tả đầy đủ:<br /></strong>
    <?php

		$CKEditor_new->editor("mota_daydu","");
	?><br />

	<strong>- Thông số kỹ thuật:<br /></strong>
    <?php
		$kt = new xl_loaitrang();
		$thongsokt = $kt->Load_FormThongSoKyThuat();
		$CKEditor_KT->editor("thongso_kythuat",$thongsokt['noidung']);
	?><br />




    <strong>- Tags:<br /></strong>

    <textarea name="tags" style="width:670px; height:100px" class="border_radius"></textarea><br /><br />



    <strong>- Mô tả:<br /></strong>

    <textarea name="txtmota" style="width:670px; height:100px" class="border_radius"></textarea>

    <div style="display:block; text-align:center">

    	 <input type="submit" name="btn_HoanTat" value="Hoàn tất" class="button medium blue">

         <a href="trangchu.php?key=alb" class="button medium blue" target="_blank">Thư viện hình ảnh</a>

    </div>



    </div>



<?php

	endif;

endif;

function up_hinh()

{

	$result = "";

	define('UPLOAD_DIR','../../images/sanpham/');

	define('MAX_FILES_SIZE',10000000);

	$file = str_replace(" ","_",$_FILES['up_hinh']['name']);

	//convert maximun size to KB

	$max = number_format(MAX_FILES_SIZE/1024,1)." KB";

	//create an array of permitted MIME types

	$premitted = array('image/gif','image/jpeg','image/pjpeg','image/png');

	//begin by assuming the file is unacceptable

	$sizeOK = false;

	$typeOK = false;

	$success = false;

	$kq=array(3);

	//Check that file is within the permitted size

	if($_FILES['up_hinh']['size']>0 && $_FILES['up_hinh']['size']<= MAX_FILES_SIZE)

	{

		$sizeOK = true;

	}



	//Check that file is within the permitted size

	foreach($premitted as $type)

	{

		if($type == $_FILES['up_hinh']['type'])

		{

			$typeOK = true;

			break;

		}

	}



	if($sizeOK && $typeOK)

	{

		switch($_FILES['up_hinh']['error'])

		{

			case 0:

				if(!file_exists(UPLOAD_DIR.$file))

				{

					$success = move_uploaded_file($_FILES['up_hinh']['tmp_name'],UPLOAD_DIR.$file);

					$kq[1]=$file;

					$kq[0] = UPLOAD_DIR;



				}

				else


				{

					$now  = date("YmdHis",time());

					$file = $now.$file;

					$success = move_uploaded_file($_FILES['up_hinh']['tmp_name'],UPLOAD_DIR.$file);

					$kq[1]=$file;

					$kq[0] = UPLOAD_DIR;

				}

				if($success){

					$result = "$file upload thành công";

					//$result = "$file uploaded successfully";

				}

				else{

					$result = "Lỗi upload $file.Vui long thử lại.";

					//$result = "Error uploading $file.Please try again.";

				}

				break;

			case 3:

				$result = "Lỗi upload $file.Vui long thử lại.";

				//$result = "Error uploading $file.Please try again.";

			default:

				$result = "Lỗi hệ thống upload $file.Vui long thử lại.";

				//$result = "System error uploadding $file contact webmaster";

		}

	}

	elseif($_FILES['up_hinh']['error']==4){

		$result = "Chưa chọn file upload";

		//$result = "No file selected";

	}

	else{

		$result = "Lỗi file upload. Maximum size: $max. File upload phải là gif, jpg, png.";

		//$result ="$file cannot be uploaded. Maximum size: $max. Acceptable file types: gif, jpg, png.";

	}

	if($success == TRUE)

	{

		echo '<script language="javascript">

				window.alert("'.$result.'");

			</script>';

		//echo '<meta http-equiv="refresh" content="2;url=them_tintuc.php"  />';

	}
	else
	{
		echo '<script language="javascript">
				window.alert("'.$result.'");
			</script>';
		//echo '<meta http-equiv="refresh" content="2;url=them_tintuc.php"  />';
	}
	$kq[2] = $success;
	return $kq;
}
 ?>

</form>

</div>